package client.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EtchedBorder;

import client.network.Client;

public class StartUpGUI extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5830213836199833351L;
	private JTextField connection;
	private JTextField userName;
	private JButton connectButton;
	private JLabel nameInstruction;
	private JLabel addressInstruction;
	private int hightPadding = 10;
	private int widthPadding = 10;
	private Client client;

	public StartUpGUI() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		client = new Client();
		connection = new JTextField("", 20);
		userName = new JTextField("Player", 20);
		connectButton = new JButton("Connect");
		connectButton.addActionListener(this);
		connection.addActionListener(this);
		addressInstruction = new JLabel("Enter servers IP-adress: ");
		nameInstruction = new JLabel("Enter a name: ");

		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		add(Box.createVerticalStrut(hightPadding), c);
		c.gridy = 1;
		add(Box.createHorizontalStrut(widthPadding), c);
		c.gridx = 1;

		add(getFields(), c);

		c.gridy++;
		add(Box.createVerticalStrut(hightPadding / 2), c);
		c.gridy++;
		c.gridx = 1;
		c.gridwidth = 2;
		add(connectButton, c);
		c.gridwidth = 1;
		c.gridx = 3;
		add(Box.createHorizontalStrut(widthPadding), c);
		c.gridy++;
		add(Box.createVerticalStrut(hightPadding), c);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
	}

	private JPanel getFields() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.LINE_END;
		panel.add(nameInstruction, c);
		c.gridx = 1;
		panel.add(userName, c);
		c.gridy = 1;
		c.gridx = 0;
		panel.add(addressInstruction, c);
		c.gridx = 1;
		panel.add(connection, c);

		panel.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Login"));

		return panel;
	}

	public static void main(String[] args) {
		new StartUpGUI();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == connectButton || e.getSource() == connection) {
			String ip = connection.getText();
			String name = userName.getText();
			if (client.connect(ip, name)) {
				this.dispose();
			} else {
				JOptionPane.showMessageDialog(null, "Invalid IP-adress");
			}
		}
	}

}
